<?php
/**
 * This file contains the frontend class
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: frontend.class.php 102 2010-07-13 17:13:50Z sruttloff@googlemail.com $
 * @link      phpmy
 */

/**
 * class frontnend
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class frontend
{
    /**
     * @var object instance
     */
    protected static $INSTANCE;
    /**
     * @var object Database class
     */
    protected $DB;
    /**
     * @var object i18n
     */
    protected $I18N;
    /**
     * @var object configuration
     */
    protected $CONFIG;
    /**
     * @var object user configuration
     */
    protected $USERCONFIG;
    /**
     * @var object session class
     */
    protected $SESSION = false;
    /**
     * @var string 	name of old errorhandler
     */
    public $OLDERRORHANDLER = null;
    /**
     * @var string bool true when new error handler is set
     */
    public $NEWERRORHANDLER = null;
    /**
     * @var string   name of old exception handler
     */
    public $OLDEXCEPTIONHANDLER = null;
    /**
     * @var string   name of new exception handler function
     */
    public $NEWEXCEPTIONHANDLER = null;
    /**
     * @var string bool true when new handler is set
     */
    protected $PHPFailureText = null;
    /**
     * @var	array		volatile memory
     */
    protected $MERKER = array ();
    /**
     * set a named volatile memory
     *
     * @param string $name memory name
     * @param mixed  $val  memory value
     *
     * @return	void
     */
    public function setMEM($name = null, $val = null)
    {
        $this->MERKER[$name] = $val;
    }
    /**
     * set a named volatile memory
     *
     * @param string $name vol. memory name
     *
     * @return mixed value of mem.
     */
    public function getMEM($name = null)
    {
        return $this->MERKER[$name];
    }
    /**
     * instantiate essential objects and make them avail. as subobjects
     *
     * @param string $sessionname session name (optional)
     *
     * @return	void
     */
    private function __construct($sessionname = 'PHPSESSION')
    {
        $this->CONFIG = config::getInstance();
        $this->DB = database::getInstance();;
        if (!is_object($this->SESSION))
            $this->SetSession(session::getInstance($sessionname));
    }
    /**
     * do some after construct, when object is existing
     *
     * @return void
     */
    protected function afterConstruct()
    {
        $this->InitUserconfig();
        $this->setErrorHandler();
        $this->setExceptionHandler();
    }
    /**
     * restore errorhandler
     *
     * @return void
     */
    public function restoreErrorHandler ()
    {
        if ($this->OLDERRORHANDLER && $this->NEWERRORHANDLER)
        {
            set_error_handler($this->OLDERRORHANDLER);
            $this->NEWERRORHANDLER = false;
        }
    }
    /**
     * set default exc. handler
     *
     * @return void
     */
    public function setExceptionHandler ()
    {
        if ($this->NEWEXCEPTIONHANDLER)
            return;
        $this->OLDEXCEPTIONHANDLER = set_exception_handler('myExceptionHandler');
        $this->NEWEXCEPTIONHANDLER = true;
    }
    /**
     * set a new Error handler
     *
     * @param string $s_errorHandler function name of error handler function
     *
     * @return void
     */
    public function setErrorHandler ($s_errorHandler = 'myErrorHandler')
    {
        // if ($this->NEWERRORHANDLER) return;
        $this->OLDERRORHANDLER = set_error_handler($s_errorHandler);
        $this->NEWERRORHANDLER = true;
    }
    /**
     * Init userconfig if session is avail. and user is logged in
     *
     * @param int $int_userid user id to load config object
     *
     * @return  void
     */
    public function initUserconfig($int_userid = 0)
    {
        if (!$int_userid)
        {
            $int_userid = login::getInstance()->getUserId();
        }
        $this->USERCONFIG = userConfig::getInstance($int_userid);
    }
    /**
     * set a session object
     *
     * @param object $session session object
     *
     * @return void
     */
    public function setSession($session)
    {
        if (!is_object($this->SESSION))
            $this->SESSION = session::getInstance();
        return;
    }
    /**
     * return a instance of object
     *
     * @param string $sessionname session name (optional)
     *
     * @return frontend instance
     */
    public static function getInstance($sessionname = "PHPSESSION")
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c ($sessionname);
            self::$INSTANCE->afterConstruct();
        }
        return self::$INSTANCE;
    }
    /**
     * set i18 object
     *
     * @param object $object instance of i18n
     *
     * @return void
     */
    public function setI18n($object = null)
    {
        $this->I18N = $object;
        return;
    }
    /**
     * get i18 object
     *
     * @return i18n
     */
    public function getI18n()
    {
        return $this->I18N;
    }
    /**
     * get config object
     *
     * @return config
     */
    public function getConfig()
    {
        return $this->CONFIG;
    }
    /**
     * get user config object
     *
     * @return userConfig
     */
    public function getUserConfig()
    {
        return $this->USERCONFIG;
    }
    /**
     * get DB object
     *
     * @return database
     */
    public function getDB()
    {
        return $this->DB;
    }
    /**
     * get session object
     *
     * @return session
     */
    public function getSession()
    {
        return $this->SESSION;
    }
}